package com.cci.kangdao.controller;

import com.alibaba.fastjson.JSON;
import com.cci.kangdao.crmapi.CRMServiceConst;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.service.OutSideService;
import com.cci.kangdao.utilTool.PublicTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @description:
 * @author: shiliqiang
 * @company: CTY Technology Co.,Ltd
 * @since: 2021/7/28 8:43
 * @version: V1.0
 */
@Controller
@RequestMapping("/outside/")
public class OutSideInterfaceController {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Autowired
    private FaultController faultController;
    @Autowired
    private CRMPlatformController cRMPlatformController;

    @Autowired
    private OutSideService outSideService;


    @RequestMapping(value = "getPictures.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    //@ResponseBody
    public void getPictures(@RequestBody String str, /*@RequestHeader String token*/ @RequestHeader HttpHeaders headers, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        String orderNo = (String) map.get("orderNo");
        String systemTime =  (String) map.get("systemTime");
        String company = (String) map.get("company");
        if (StringUtils.isEmpty(orderNo)) {
            cRMPlatformController.missingNecessaryParams(response, "缺少请求参数");
            return;
        }
        if (StringUtils.isEmpty(systemTime) || StringUtils.isEmpty(company)) {
            cRMPlatformController.missingNecessaryParams(response, "缺少请求参数");
            return;
        }

        String md5ToToken = PublicTool.compute(String.format("%s+%s", company, systemTime));
        String token = headers.getFirst("token");
        if (!md5ToToken.equals(token)) {
            cRMPlatformController.missingNecessaryParams(response, "请求参数传递不正确");
            return;
        }
        // 转成工单id
        Map<String, Object> orderInfoMap = outSideService.getOrderInfoByOrderNo(orderNo);
        if (orderInfoMap == null || orderInfoMap.size() <= 0) {
            cRMPlatformController.missingNecessaryParams(response, "请求参数传递不正确");
            return;
        }
        Long orderId = (Long) orderInfoMap.get("ID");
        map.put("orderId", orderId);
        faultController.getPictures(map.toString(), response);
    }
}
